package com.surveypark.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.id.UUIDHexGenerator;
import org.springframework.stereotype.Service;

import com.surveypark.dao.BaseDao;
import com.surveypark.domain.Log;
import com.surveypark.service.LogService;
import com.surveypark.util.LogUtil;

@Service("logService")
public class LogServiceImpl extends BaseServiceImpl<Log> implements
		LogService {

	@Resource(name="logDao")
	public void setDao(BaseDao<Log> dao) {
		super.setDao(dao);
	}
	
	@Override
	public void saveEntity(Log t) {
		//选择向不同的日志表中插入数据，只能使用sql语句来实现
		String sql = "insert into " + LogUtil.generateLogTabName(0) + "(id, operator, opertime, opername, operparams, operresult, resultmsg) values(?,?,?,?,?,?,?)";
		UUIDHexGenerator uuid = new UUIDHexGenerator();
		String id = (String) uuid.generate(null, null);
		this.executeSQL(sql, 
						id,
						t.getOperator(),
						t.getOperTime(),
						t.getOperName(),
						t.getOperParams(),
						t.getOperResult(),
						t.getResultMsg());
	}

	/**
	 * 查询最近的日志信息，默认两个月,当前月与下个月
	 */
	@Override
	public List<Log> findNearestLogs() {

		String sql = "select * from " + LogUtil.generateLogTabName(-1)
					 + " union "
					 + " select * from " + LogUtil.generateLogTabName(0);
		return this.findObjectBySQL(sql);
	}
}
